home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
1035
/
1035.xpi
/
chrome
/
1clickweather.jar
/
content
/
1clickweather
/
js
/
utils
/
filemanager.js
< prev
next >
Wrap
Text File
|
2008-10-05
|
13KB
|
345 lines
// ⌐ 2005 The Weather Channel Interactive, Inc. All Rights Reserved.
/*
FileManager - manager file for file manipulation
*/
function FileManager() {
this.ProfileDir = "ProfD";
this.configFolder = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get(this.ProfileDir, Components.interfaces.nsIFile);
this.configFolder.append("extensions");
this.configFolder.append(EXTENSIONID);
this.configFolder.append("chrome");
this.configFolder.append("config");
this.appConfigFile = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get(this.ProfileDir, Components.interfaces.nsIFile);
this.appConfigFile.append("extensions");
this.appConfigFile.append(EXTENSIONID);
this.appConfigFile.append("chrome");
this.appConfigFile.append("config");
this.appConfigFile.append(APPCONFIGFILENAME);
this.userConfigFile = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get(this.ProfileDir, Components.interfaces.nsIFile);
this.userConfigFile.append("extensions");
this.userConfigFile.append(EXTENSIONID);
this.userConfigFile.append("chrome");
this.userConfigFile.append("config");
this.userConfigFile.append(USERCONFIGFILENAME);
this.defaultAppConfigFile = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get(this.ProfileDir, Components.interfaces.nsIFile);
this.defaultAppConfigFile.append("extensions");
this.defaultAppConfigFile.append(EXTENSIONID);
this.defaultAppConfigFile.append("chrome");
this.defaultAppConfigFile.append("config");
this.defaultAppConfigFile.append(DEFAULT_APPCONFIGFILENAME);
this.defaultUserConfigFile = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get(this.ProfileDir, Components.interfaces.nsIFile);
this.defaultUserConfigFile.append("extensions");
this.defaultUserConfigFile.append(EXTENSIONID);
this.defaultUserConfigFile.append("chrome");
this.defaultUserConfigFile.append("config");
this.defaultUserConfigFile.append(DEFAULT_USERCONFIGFILENAME);
this.fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
this.sstream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
this.foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
this.domSerializer = Components.classes["@mozilla.org/xmlextras/xmlserializer;1"].createInstance(Components.interfaces.nsIDOMSerializer);
};
/*
FileManager prototype implementation
*/
FileManager.prototype = {
configFolder : null,
appConfigFile : null,
userConfigFile : null,
defaultAppConfigFile : null,
defaultUserConfigFile : null,
fstream : null,
sstream : null,
foStream : null,
domSerializer : null,
/*
makes sure that if older versions of the user config files exist, to update the new ones with
the old ones, then delete the old ones so that the update does not happen again
*/
updateUserConfigFiles : function() {
try {
var i = 0;
var old_userConfigFile;
var old_userConfigFileToUse;
var found = 0;
for(i=0;i<PREVIOUSUSERCONFIGFILES.length;i++) {
//alert("NEXT TO LOOK: " + PREVIOUSUSERCONFIGFILES[i]);
old_userConfigFile = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get(this.ProfileDir, Components.interfaces.nsIFile);
old_userConfigFile.append("extensions");
old_userConfigFile.append(EXTENSIONID);
old_userConfigFile.append("chrome");
old_userConfigFile.append("config");
old_userConfigFile.append(PREVIOUSUSERCONFIGFILES[i]);
if(old_userConfigFile.exists()) {
//alert("IT EXISTS!!!");
old_userConfigFileToUse = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get(this.ProfileDir, Components.interfaces.nsIFile);
old_userConfigFileToUse.append("extensions");
old_userConfigFileToUse.append(EXTENSIONID);
old_userConfigFileToUse.append("chrome");
old_userConfigFileToUse.append("config");
old_userConfigFileToUse.append(PREVIOUSUSERCONFIGFILES[i]);
found = 1;
} else {
//alert("IT DOES NOT EXIST!!");
}
}
if(found > 0) {
// the old file exists..so we need to delete the "new" file first and then rename the "old" file
this.userConfigFile.remove(false);
old_userConfigFileToUse.copyTo(this.configFolder, USERCONFIGFILENAME);
// ok now remove the old one so this wont happen again!
old_userConfigFileToUse.remove(false);
} else {
//alert("NOTHING FOUND TO COPY OVER!!");
}
} catch(e) {
//alert("ERROR: " + e);
}
},
/*
returns the list of files in the "config" directory
*/
getConfigFolderFileList : function() {
var entries = this.configFolder.directoryEntries;
var files = new Array();
while (entries.hasMoreElements()) {
var entry = entries.getNext();
entry = entry.QueryInterface(Components.interfaces.nsIFile);
var name = entry.leafName;
}
return entries;
},
/*
returns raw content of the app config data file
*/
getAppConfigFileData : function() {
var data = "";
this.fstream.init(this.appConfigFile, 1, 0, false);
this.sstream.init(this.fstream);
var str = this.sstream.read(-1);
while (str.length > 0) {
data += str;
str = this.sstream.read(-1);
}
this.sstream.close();
this.fstream.close();
return data;
},
/*
returns raw content of the default app config data file!
*/
getDefaultAppConfigFileData : function() {
var data = "";
this.fstream.init(this.defaultAppConfigFile, 1, 0, false);
this.sstream.init(this.fstream);
var str = this.sstream.read(-1);
while (str.length > 0) {
data += str;
str = this.sstream.read(-1);
}
this.sstream.close();
this.fstream.close();
return data;
},
/*
returns dom representation of the app config data file
*/
getAppConfigDOM : function() {
var xmlDoc = document.implementation.createDocument("", "", null);
if (xmlDoc.readyState == null) {
xmlDoc.readyState = 1;
xmlDoc.addEventListener("load", function () {
xmlDoc.readyState = 4;
if (typeof xmlDoc.onreadystatechange == "function")
xmlDoc.onreadystatechange();
}, false);
}
xmlDoc.async=false;
xmlDoc.loadXML(this.getAppConfigFileData());
// we need to check if xml that we got is "valid"!!!!
// to do that we check the root element and/or the document namespace
var roottag = xmlDoc.documentElement;
if ((roottag.tagName == "parserError") || (roottag.namespaceURI == "http://www.mozilla.org/newlayout/xml/parsererror.xml")){
// not well-formed xml was entered--in this case only thing we can do is get the "default"
// configuration and use it!!
//alert("using default appconfig info!");
xmlDoc.loadXML(this.getDefaultAppConfigFileData());
}
return xmlDoc;
},
/*
stores app config file from text
*/
storeAppConfigFromText : function(data) {
this.foStream.init(this.appConfigFile, 0x02 | 0x08 | 0x40, 420, 0);
this.foStream.write(data, data.length);
this.foStream.close();
},
/*
stores app config file from DOM object
*/
storeAppConfigFromDOM : function(domdata) {
this.foStream.init(this.appConfigFile, 0x02 | 0x08 | 0x40, 420, 0);
var strdata = domdata.xml;
this.foStream.write(strdata, strdata.length);
this.foStream.close();
},
/*
returns raw content of the user config data file
*/
getUserConfigFileData : function() {
var data = "";
this.fstream.init(this.userConfigFile, 1, 0, false);
this.sstream.init(this.fstream);
var str = this.sstream.read(-1);
while (str.length > 0) {
data += str;
str = this.sstream.read(-1);
}
this.sstream.close();
this.fstream.close();
return data;
},
/*
returns raw content of the default user config data file
*/
getDefaultUserConfigFileData : function() {
var data = "";
this.fstream.init(this.defaultUserConfigFile, 1, 0, false);
this.sstream.init(this.fstream);
var str = this.sstream.read(-1);
while (str.length > 0) {
data += str;
str = this.sstream.read(-1);
}
this.sstream.close();
this.fstream.close();
return data;
},
/*
returns dom representation of the user config data file
*/
getUserConfigDOM : function() {
var xmlDoc = document.implementation.createDocument("", "", null)
if (xmlDoc.readyState == null) {
xmlDoc.readyState = 1;
xmlDoc.addEventListener("load", function () {
xmlDoc.readyState = 4;
if (typeof xmlDoc.onreadystatechange == "function")
xmlDoc.onreadystatechange();
}, false);
}
xmlDoc.async=false;
try{
xmlDoc.loadXML(this.getUserConfigFileData());
}catch(e){ // if we can't load the real on, load the default
xmlDoc.loadXML(this.getDefaultUserConfigFileData());
return(xmlDoc);
}
// we need to check if xml that we got is "valid"!!!!
// to do that we check the root element and/or the document namespace
var roottag = xmlDoc.documentElement;
if ((roottag.tagName == "parserError") || (roottag.namespaceURI == "http://www.mozilla.org/newlayout/xml/parsererror.xml")){
// not well-formed xml was entered--in this case only thing we can do is get the "default"
// configuration and use it!!
xmlDoc.loadXML(this.getDefaultUserConfigFileData());
//alert("using default userconfig info!");
}
return xmlDoc;
},
/*
stores user config file
*/
storeUserConfigFromText : function(data) {
var date = new Date();
// first move the current file to a backup
this.userConfigFile.remove(false);
// now create the new file
this.foStream.init(this.userConfigFile, 0x02 | 0x08 | 0x40, 420, 0);
this.foStream.write(data, data.length);
this.foStream.close();
},
/*
stores user config file
*/
storeUserConfigFromDOM : function(domdata) {
this.foStream.init(this.userConfigFile, 0x02 | 0x08 | 0x40, 420, 0);
var strdata = domdata.xml;
this.foStream.write(strdata, strdata.length);
this.foStream.close();
},
/*
returns representation of config directory
*/
getConfigFolder : function() {
return this.configFolder;
},
saveUserConfigToFile : function(node) {
var fileOutputStream =
Components.classes["@mozilla.org/network/file-output-stream;1"].
createInstance(Components.interfaces.nsIFileOutputStream);
fileOutputStream.init(this.userConfigFile, 0x20 | 0x02,00004,null);
this.domSerializer.serializeToStream(node, fileOutputStream, 'UTF-8');
fileOutputStream.close();
/*
var fostream = Components.classes["@mozilla.org/network/file-output-stream;1"]
.createInstance(Components.interfaces.nsIFileOutputStream);
if(fostream.isWritable()){
// 0x02 | 0x10 to open file for appending
fostream.init(this.userConfigFile,0x02|0x08|0x20,664,0); // write, create, truncate
fostream.write(node,node.length);
fostream.close();
}else{
alert("The file " + this.userConfigFile + " is not writable");
}
*/
},
saveAppConfigToFile : function(node) {
var fileOutputStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
fileOutputStream.init(this.appConfigFile, 0x20 | 0x02,00004,null);
this.domSerializer.serializeToStream(node, fileOutputStream, 'UTF-8');
fileOutputStream.close();
}
};